import datetime
from datetime import datetime

from src.main.server.controller.server_feedback_controller import ServerFeedbackController
from src.main.server.mapper.impl.server_review_impl import ServerReviewImpl
from src.main.server.pojo.Result import Result
from src.main.server.utils.format_datetime import format_datetime
from src.main.server.utils.review_judge import process_data_review


class ServerReviewController:

    def __init__(self):
        self.impl = ServerReviewImpl()

    def all_info(self):
        data = self.impl.all_info()
        # 处理数据
        formatted_data = tuple(
            tuple(format_datetime(item) for item in row)
            for row in data
        )
        return process_data_review(formatted_data)

    def get_info(self, fd, sd):
        data = self.impl.get_info(fd, sd)
        return data

    def update(self, data):
        # data = (status, review_time, server_name, vol_name)
        """
        进行项目审核意见
        若选择通过，则也为反馈表增添数据
        """
        self.impl.update(data)
        #如果通过，则新增反馈数据
        if data[0] == "1":
            #1为通过
            #2不通过
            #0未审核
            vol_name = data[3]
            server_name = data[2]
            #应先检查反馈表数据库是否已经存在该数据
            #防止数据重复
            count = ServerFeedbackController().check((vol_name, server_name))[0][0]
            if count >= 1:
                #数据已存在
                return None
            #无重复，进行数据插入
            ServerFeedbackController().insert((vol_name, server_name))
        return Result().success()

    def insert(self, ser_name, vol_name):
        """插入审核数据 项目名+志愿者名"""
        #1.检查是否数据库已有该数据
        count = self.impl.check((ser_name, vol_name))[0]
        if count > 0:
            #存在
            return None
        #2.进行数据插入
        self.impl.insert((datetime.now(), ser_name, vol_name))
        return Result().success()

    def delete_by_serverName_volName(self, data):
        self.impl.delete(data)
        return Result().success()

if __name__ == '__main__':
    result = ServerReviewController().get_info("社区服务", "李四")
    print(result)